//
//  LJJBuyCarViewController.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/14.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJBuyCarViewController.h"
#import "LJJBuyCarNoDataView.h"
#import "LJJLoginViewController.h"
#import "LJJBuyCarModel.h"
#import "LJJBuyCarTableView.h"
#import "LJJGoodsDetailViewController.h"
#import "LJJPriceView.h"
#import "LJJSureOrderRootModel.h"
#import "LJJSureOrderViewController.h"

@interface LJJBuyCarViewController ()

@property (strong, nonatomic) LJJBuyCarNoDataView *noDataView;    /** 没有数据的view **/
@property (strong, nonatomic) LJJBuyCarTableView *tableView;    /** 购物车表格 **/
@property (strong, nonatomic) LJJPriceView *priceView;    /** 价格 **/
@property (strong, nonatomic) NSArray *tableArray;    /** 表格的数组 **/

@end

@implementation LJJBuyCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getBuyCarData];
}
#pragma mark -
#pragma mark - method
- (void)configUI
{
    self.view.backgroundColor = MAINCOLOR;
    [self.view addSubview:self.noDataView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.priceView];
    __weak typeof(self) weakSelf = self;
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view.mas_top);
        make.bottom.equalTo(weakSelf.priceView.mas_top);
        make.right.left.equalTo(weakSelf.view);
    }];
    [_noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(250, 250));
    }];
    [_priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@55);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
    }];
    self.noDataView.hidden = YES;
}
- (void)getBuyCarData
{
    NSDictionary *landingDic = [[NSUserDefaults standardUserDefaults]valueForKey:@"ISLOGIN"];
    //判断是否已经登录，如果没有登录可直接跳转到登录页面
    if (landingDic.count == 0) {
        LJJLoginViewController *loginVC = [[LJJLoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }else{//如果已经登录，则加载正常的购物车页面
        [self getData:@"appShopCart/appCartGoodsList.do" param:@{@"MemberId":landingDic[@"MemberId"]} success:^(id responseObject) {
            self.tableArray = [NSMutableArray array];
            self.tableArray = [LJJBuyCarModel mj_objectArrayWithKeyValuesArray:responseObject];
            if (self.tableArray.count > 0) {
                self.tableView.buyCarModelArray = [NSMutableArray arrayWithArray:self.tableArray];
                [self countPriceMoney];
            }
            else
            {
                self.noDataView.hidden = NO;
            }
        } error:^(NSError *error) {
            LJJLog(@"BuyCarError: --- %@", error);
        }];
    }
}
- (void)countPriceMoney{
    CGFloat priceMoney = 0.0;
    for (LJJBuyCarModel *model in self.tableView.buyCarModelArray) {
        if (model.isSelectButton) {
            priceMoney += model.Price * model.GoodsCount;
        }
    }
    [self priceAttributedText:priceMoney];
    [self changeBuyCarData];
}
- (void)priceAttributedText:(CGFloat)money{
    //当前价格(需要手动添加人民币符号)
    NSString *nowPrice = @"合计：";
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:nowPrice attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:12.0]}];
    //过去价格
    NSString *oldString = [NSString stringWithFormat:@"¥%.2lf",money];
    NSMutableAttributedString *oldPrice = [[NSMutableAttributedString alloc]initWithString:oldString attributes:@{NSForegroundColorAttributeName:RGB(230, 46, 37, 1),NSFontAttributeName:[UIFont boldSystemFontOfSize:15.0]}];
    [string insertAttributedString:oldPrice atIndex:string.length];
    _priceView.priceLabel.attributedText = string;
}
- (void)changeBuyCarData{
    /*
     拼装字符串标记：updateCartMsg
     格式：购物记录UUID,商品数量#购物记录UUID，商品数量#......
     */
    NSString * buyCarData;
    NSMutableArray *buyCarDataArray = [NSMutableArray arrayWithCapacity:0];
    for (LJJBuyCarModel *model in self.tableArray) {
        buyCarData = [NSString stringWithFormat:@"%@,%ld",model.UUID,(long)model.GoodsCount];
        [buyCarDataArray addObject:buyCarData];
    }
    buyCarData = [buyCarDataArray componentsJoinedByString:@"#"];
    [self getData:@"appShopCart/appUpdateCart.do" param:@{@"updateCartMsg":buyCarData} success:^(id responseObject) {
        LJJLog(@"responseObject : %@",responseObject);
    } error:^(NSError *error) {
        LJJLog(@"updateCartError: --- %@", error);
    }];
}
- (void)gotoInsertMethod{
    [self getData:@"appOrder/gotoInsert.do" param:[self makeInsertDic] success:^(id responseObject) {
        LJJSureOrderRootModel *result = [LJJSureOrderRootModel mj_objectWithKeyValues:responseObject];
        LJJSureOrderViewController *order = [[LJJSureOrderViewController alloc]init];
        order.orderTableData = result;
        [self.navigationController pushViewController:order animated:YES];
    } error:^(NSError *error) {
        LJJLog(@"payVCError: --- %@", error);
    }];
}
- (NSDictionary *)makeInsertDic{
    NSString * buyCarData;
    NSMutableArray *buyCarDataArray = [NSMutableArray arrayWithCapacity:0];
    for (LJJBuyCarModel *model in self.tableArray) {
        buyCarData = [NSString stringWithFormat:@"%ld,%@,%lf",(long)model.GoodsCount,model.GoodsId,model.Weight];
        [buyCarDataArray addObject:buyCarData];
    }
    buyCarData = [buyCarDataArray componentsJoinedByString:@"#"];
    
    return @{@"Goods":buyCarData};
}
#pragma mark -
#pragma mark - lazy method
- (LJJBuyCarNoDataView *)noDataView
{
    if (!_noDataView) {
        _noDataView = [[LJJBuyCarNoDataView alloc] init];
        __weak typeof(self) weakSelf = self;
        _noDataView.shoppingBlock= ^(){
            weakSelf.tabBarController.selectedIndex = 0;
        };
    }
    return _noDataView;
}
- (LJJBuyCarTableView *)tableView
{

    if (!_tableView) {
        _tableView = [[LJJBuyCarTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
        __weak typeof(self) weakSelf = self;
        _tableView.detailBlock = ^(NSString *goodsID){
            LJJGoodsDetailViewController *detailVC = [[LJJGoodsDetailViewController alloc] init];
            detailVC.detailGoodsID = goodsID;
            [weakSelf.navigationController pushViewController:detailVC animated:YES];
        };
        _tableView.changeDataBlock = ^(){
            [weakSelf countPriceMoney];
        };
    }
    return _tableView;
}
- (LJJPriceView *)priceView
{
    if (!_priceView) {
        _priceView = [[LJJPriceView alloc] init];
        __weak typeof(self) weakSelf = self;
        _priceView.payBlock = ^(){
            [weakSelf gotoInsertMethod];
        };
    }
    return _priceView;
}

@end
